package com.spiro;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.apache.zookeeper.ZooDefs.Ids.CREATOR_ALL_ACL;

/**
 * Created by Administrator on 2016/10/16 0016.
 */
public class ACLsTest {
    public static void main(String[] args) throws KeeperException, InterruptedException {
        ZooKeeper zooKeeper = null;

        try {
            zooKeeper = new ZooKeeper("192.168.1.105:2181", 10000, new Watcher() {
                public void process(WatchedEvent event) {
                    System.out.println(event);
                }
            });

            zooKeeper.addAuthInfo("digest", "spiro:123".getBytes());

//            List<ACL> acls = zooKeeper.getACL("/test-acl-auth-2", new Stat());
//            System.out.println(acls);
//
//            byte[] bytes = zooKeeper.getData("/test-acl-auth-2", false, new Stat());
//            System.out.println("getData -> " + new String(bytes));

            List<ACL> acls = new ArrayList<ACL>(CREATOR_ALL_ACL);
            acls.add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE));
            zooKeeper.create("/test-acl-auth-3", "acl test".getBytes(), acls, CreateMode.PERSISTENT);
            System.out.println("Create path with acls ok!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zooKeeper != null) {
                try {
                    zooKeeper.close();
                } catch (InterruptedException e) {
                }
            }
        }
    }
}
